﻿using ECGadmin.WebUI.Attributes;
using ECGadmin.WebUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace ECGadmin.WebUI.Controllers
{
    [AdminLoggedAuthorize]
    public class ConfigSystemController : Controller
    {
        //
        // GET: /ConfigSystem/

        public ActionResult Index()
        {
            var xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("~/ECGsystem.xml"));
            var model = new ECGsystemModel();
            model.AnalyticsCode = Server.HtmlDecode(xmldoc.SelectSingleNode("/ECGsystem/AnalyticsCode").InnerText);
            model.WebmastertoolCode = Server.HtmlDecode(xmldoc.SelectSingleNode("/ECGsystem/WebmastertoolCode").InnerText);
            model.Domainname = xmldoc.SelectSingleNode("/ECGsystem/Domainname").InnerText;
            model.EmailSystem = xmldoc.SelectSingleNode("/ECGsystem/EmailSystem").InnerText;
            model.PassEmailSystem = xmldoc.SelectSingleNode("/ECGsystem/EmailSystem").Attributes["Password"].Value;
            model.EmailBcc = xmldoc.SelectSingleNode("/ECGsystem/EmailBcc").InnerText;
            model.EmailCc = xmldoc.SelectSingleNode("/ECGsystem/EmailCc").InnerText;
            model.HeaderMeta = Server.HtmlDecode(xmldoc.SelectSingleNode("/ECGsystem/HeaderMeta").InnerText);
            model.WebsiteName = xmldoc.SelectSingleNode("/ECGsystem/WebsiteName").InnerText;
            model.FacebookFanpageUrl = xmldoc.SelectSingleNode("/ECGsystem/FacebookFanpageUrl").InnerText;
            model.FacebookCode = Server.HtmlDecode(xmldoc.SelectSingleNode("/ECGsystem/FacebookCode").InnerText);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(ECGsystemModel model)
        {
            var xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("~/ECGsystem.xml"));
            xmldoc.SelectSingleNode("/ECGsystem/AnalyticsCode").InnerText = Server.HtmlEncode(model.AnalyticsCode);
            xmldoc.SelectSingleNode("/ECGsystem/WebmastertoolCode").InnerText = Server.HtmlEncode(model.WebmastertoolCode);
            xmldoc.SelectSingleNode("/ECGsystem/Domainname").InnerText = model.Domainname;
            xmldoc.SelectSingleNode("/ECGsystem/EmailSystem").InnerText = model.EmailSystem;
            xmldoc.SelectSingleNode("/ECGsystem/EmailSystem").Attributes["Password"].Value = model.PassEmailSystem;
            xmldoc.SelectSingleNode("/ECGsystem/EmailBcc").InnerText = model.EmailBcc;
            xmldoc.SelectSingleNode("/ECGsystem/EmailCc").InnerText = model.EmailCc;
            xmldoc.SelectSingleNode("/ECGsystem/HeaderMeta").InnerText = Server.HtmlEncode(model.HeaderMeta);
            xmldoc.SelectSingleNode("/ECGsystem/WebsiteName").InnerText = model.WebsiteName;
            xmldoc.SelectSingleNode("/ECGsystem/FacebookFanpageUrl").InnerText = model.FacebookFanpageUrl;
            xmldoc.SelectSingleNode("/ECGsystem/FacebookCode").InnerText = Server.HtmlEncode(model.FacebookCode);
            xmldoc.Save(Server.MapPath("~/ECGsystem.xml"));
            return RedirectToAction("Index", "ConfigSystem");
        }
    }
}
